<?php

/*
 * class TABLE
 * 
 * erstellt am 25.Mai.2007
 * von Michael Schramm
 *
 * Verwendung:
 *    + add_head($string, $colspan=0, $add="")
 *    + add_cell($string, $colspan=0, $add="", $nrow=false, $x=NULL, $y=NULL)
 *    + generate_txt()
 *    + generate_table()
 */


DEFINE("COLSPAN", "--colspan--");
DEFINE("SROW", "");
//DEFINE("SROW", "class=\"table_srow\"");
DEFINE("CHEAD", "");

$debug = false;



class TABLE{
	protected $size=array(0,0);	//(x, y)
	private $pos=array(0,0);	//(x, y)
	private $value=NULL;		//[y][x]
	private $row=NULL;
	private $width=NULL;
	private $head=NULL;
	private $srow=NULL;
	private $debug=false;
	
	public $table_op;
	
	
	public function TABLE(){
		global $debug;
		if(isset($debug)) $this->debug=$debug;
		
		$this->value=array();
		if($this->debug) echo "NEUE TABELLE ERSTELT\n";
		
		$this->table_op['TABLE']['B']=array("<table border=\"0\" cellspacing=\"1\" cellpadding=\"3\" style=\"width:100%;\">", "</table>");
		$this->table_op['TABLE']['HEAD']=array(array("<th ",">"), "</th>");
		$this->table_op['TABLE']['ROW']=array(array("<tr ",">"), "</tr>");
		$this->table_op['TABLE']['CELL']=array(array("<td ",">"), "</td>");
		
		$this->table_op['TXT']['B']=array("-----TABELLEN START-----\n", "-----TABELLEN ENDE-----\n");
		$this->table_op['TXT']['ROW']=array("", "\n");
	}
	protected function new_line(){
		$this->pos[1]++;
		if($this->pos[1]>$this->size[1]) $this->size[1]=$this->pos[1];
		$this->pos[0]=0;
		return $this->pos[1];
	}
	public function add_head($string, $colspan=0, $add=""){
		for($i=1; $i<$colspan; $i++) {
			$this->head[]=COLSPAN;
			if($this->debug) echo "\nHead (Zelle ".(count($this->head)-1)."): --colspan--";
		}
		$this->head[]=array($string, $add);
		if($this->debug) echo "\nHead (Zelle ".(count($this->head)-1)."): ".$string;
		if(count($this->head)-1>$this->size[0]) $this->size[0]=count($this->head)-1; 
		if(1>$this->size[1]) $this->size[1]=1; 
		return count($this->head)-1;
	}
	public function add_cell($string, $colspan=0, $add="", $nrow=false, $x=NULL, $y=NULL){
		if($x!=NULL) $this->pos[0]=$x;
		if($y!=NULL) $this->pos[1]=$y;
		if($nrow) $this->new_line();
		if($this->pos[0]+1>$this->size[0]) $this->size[0]=$this->pos[0]+1;
		if(1>$this->size[1]) $this->size[1]=1;
		for($i=1; $i<$colspan; $i++) {
			$this->value[$this->pos[1]][$this->pos[0]]=COLSPAN;
			if($this->debug) echo "\nZelle (".$this->pos[0].','.$this->pos[1]."): --colspan--";
			$this->pos[0]++;
		}
		$this->value[$this->pos[1]][$this->pos[0]]=array($string, $add);
		
		if($this->debug) echo "\nZelle (".$this->pos[0].','.$this->pos[1].") ".$this->size[0].": ".$string;
		$this->pos[0]++;
		return "(".$this->pos[0].','.$this->pos[1].')';
	}
	public function getCurrentRow(){
		return $this->pos[1];
	}
	public function add_row_style($id, $style){
		if(is_numeric($id))
			$this->row[$id]=$style;
	}
	private function row($type, $row, $t, $id){
		$table_op=$this->table_op;
		if(is_array($table_op[$type]['ROW'][0])){
			echo $table_op[$type]['ROW'][0][0];
			if($t!='HEAD' && isset($this->row[$id]))
				echo "id=\"trow_".$id."\" style=\"".$this->row[$id]."\" ";
			echo CHEAD.$table_op[$type]['ROW'][0][1];
		}else
			echo $table_op[$type]['ROW'][0];
			
		$c=0;	
		$colspan=0;
		
		if($t=='HEAD'){
			$type_n='HEAD';
			foreach($row as $string){
				$c++;
				if($string==COLSPAN) {
					$colspan++;
					continue;
				}
				
				if($colspan>0){
					#for($i=$colspan;$i>0;$i--)
					#	echo "\t ".$i."->\t|";
					if($type=="TXT"){
						
						for($i=$colspan; $i>0; $i--)
							echo "\t\t| ";
						echo $string[0]." \t| ";
					}else{
						echo "\n";
						if(is_array($table_op[$type][$type_n][0])){
							echo $table_op[$type][$type_n][0][0].$string[1]." colspan=\"".($colspan+1)."\"".$table_op[$type][$type_n][0][1];
						}else
							echo $table_op[$type][$type_n][0];
						
						echo $string[0];
						echo $table_op[$type][$type_n][1];
					}
					
					$colspan=0;
					#echo "\t ".$string."\t|";
				}else{
					if($type=="TXT"){
						echo "\t ".$string[0]."\t|";
					}else{
						echo "\n";
						if(is_array($table_op[$type][$type_n][0])){
							echo $table_op[$type][$type_n][0][0].$string[1].$table_op[$type][$type_n][0][1];
						}else
							echo $table_op[$type][$type_n][0];
						
						echo $string[0];
						echo $table_op[$type][$type_n][1];
					}
				}
			}
			
		
			
			
		}else{
			//TABELLE AUSGEBEN
			//Koerper ausgeben:
			$type_n='CELL';
			foreach($row as $string){
				$c++;
				if($string==COLSPAN) {
					$colspan++;
					continue;
				}
				
				if($colspan>0){
					#for($i=$colspan;$i>0;$i--)
					#	echo "\t ".$i."->\t|";
					if($type=="TXT"){
						for($i=$colspan; $i>0; $i--)
							echo "\t\t| ";
						echo $string[0]." \t| ";
					}else{
						if(is_array($table_op[$type][$type_n][0])){
							echo $table_op[$type][$type_n][0][0].$string[1]." colspan=\"".($colspan+1)."\"".$table_op[$type][$type_n][0][1];
						}else
							echo $table_op[$type][$type_n][0];
						
						echo $string[0];
						echo $table_op[$type][$type_n][1];
					}
					
					$colspan=0;
					#echo "\t ".$string[0]."\t|";
				}else{
					if($type=="TXT"){
						echo "\t ".$string[0]."\t|";
					}else{
						if(is_array($table_op[$type][$type_n][0])){
							echo $table_op[$type][$type_n][0][0].$string[1].$table_op[$type][$type_n][0][1];
						}else
							echo $table_op[$type][$type_n][0];
						
						echo $string[0];
						echo $table_op[$type][$type_n][1];
					}
				}
			}
		}
		
		if($this->size[0]-$c>0)
			if($type=="TXT")
				echo " NOCH ".($this->size[0]-$c)." ZELLEN\n";
			else
				for($i=$c; $i<$this->size[0]; $i++){
					if(is_array($table_op[$type][$type_n][0])){
						echo $table_op[$type][$type_n][0][0].$table_op[$type][$type_n][0][1];
					}else
						echo $table_op[$type][$type_n][0];
					
					echo " ";
					echo $table_op[$type][$type_n][1];
				}
		
		echo $table_op[$type]['ROW'][1];
	}
	private function output_tt($type){
		if($this->size[0]==0 || $this->size[1]==0){
			return "KEINE TABELLE EINGEGEBEN";
		}
		
		$table_op=$this->table_op;
		$colspan=0;
		if(!is_array($this->width) || (is_array($this->width)&& count($this->width)-1==$this->size[0])){
			$v=floor(100/$this->size[0]);
			$this->width=array();
			for($i=0;$i<$this->size[0];$i++)
				$this->width[]=$v."%";
		}
		
		ob_start();
		//HEAD ausgeben
		
		echo $table_op[$type]['B'][0];
		$c=0;
		
		if(is_array($this->head))
			$this->row($type, $this->head, "HEAD", $c);
		
		foreach($this->value as $data){
			$this->row($type, $data, "DATA", $c);
			$c++;
		}
		echo $table_op[$type]['B'][1];
		
		$out=ob_get_contents();
		ob_end_clean();	
		return $out;
	}
	public function generate_txt(){
		return $this->output_tt('TXT');
	}
	public function generate_table(){
		return $this->output_tt('TABLE');
	}
}

?>
